import {request} from "../../request/index.js"
// pages/goods_list/index.js
Page({
  data: {
    tabs:[
      {
        id:0,
        value: '综合',
        isActive: true,
      },{
        id:1,
        value: '价格',
        isActive: false,
      },{
        id:2,
        value: '销量',
        isActive: false,
      },
    ],
    goodsList:[]
  },
  //接口要的参数
  QueryParams:{
    query:'',
    cid:'',
    pagenum:1,
    pagesize:10
  },totalPages:1,
  //标题的点击事件从子组件传递过来的
  handletabsItemChange(e){
    console.log(e)
    //获取被点击的标题索引
    const {index}=e.detail;
    //2.修改原数组
    let tabs=this.data.tabs;
    //v:循环项，i:循环项的索引
    tabs.forEach((v,i)=>{
      i===index ? v.isActive =true : v.isActive =false;
      this.setData({
        tabs
      })
    })
  },
  onLoad: function (options) {
    // console.log(options)
    this.QueryParams.cid=options.cid;
    this.getGoodsList()
  },
  //获取商品列表
  async getGoodsList( ){
    request({url:"/goods/search"})
      .then(result=>{
        console.log(result)
       const total = result.data.message.total
       this.totalPages=Math.ceil(total/this.QueryParams.pagesize)
       console.log(this.totalPages)
        this.setData({
          goodsList:result.data.message.goods
          // goodsList:[...this.data.getGoodsList,...result.data.message.goods]
        })
      })
      // const res=await request({
      //   url:"/goods/search",
      //   data:this.QueryParams
      // })
      // console.log(res)
  },
  //页面上滑触底事件
  onReachBottom(){
    console.log("已经触底了哦")
    if(this.QueryParams.pagenum>=this.totalPages){
      console.log("没有下一页了")
    }else{
      console.log("有下一页数据")
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  }
})